

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;
using JTC.MiniCommerce.Store.Web.Controls;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web.admin.controls.order {
  public partial class notes : AdminControl {
  
    #region Member Variables
    
    private int orderId = 0;
    private string view = string.Empty;

    #endregion
    
    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        orderId = Utility.GetIntParameter("orderId");
        view = Utility.GetParameter("view");
        if(orderId > 0 && view == "n") {
          SetNotesProperties();
          if(!Page.IsPostBack) {
            LoadOrderNotes();
          }
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(notes).Name + ".Page_Load", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click(object sender, EventArgs e) {
      if(!string.IsNullOrEmpty(txtNote.Text)) {
        try {
          OrderNote orderNote = new OrderNote();
          orderNote.OrderId = orderId;
          orderNote.Note = txtNote.Text.Trim();
          orderNote.Save(WebUtility.GetUserName());
          LoadOrderNotes();
          txtNote.Text = string.Empty;
          base.MasterPage.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblOrderNoteSaved"));
        }
        catch(Exception ex) {
          Logger.Error(typeof(notes).Name + ".btnSave_Click", ex);
          base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
        }
      }
    }
    
    #endregion
    
    #region Methods
    
    #region Private

    /// <summary>
    /// Sets the notes properties.
    /// </summary>
    private void SetNotesProperties() {
      LocalizationUtility.AddPanelText(pnlNoteList);
      LocalizationUtility.AddPanelText(pnlAddNote);
      LocalizationUtility.AddLabelText(lblAddNote);
      LocalizationUtility.AddButtonText(btnSave);
    }

    /// <summary>
    /// Loads the order notes.
    /// </summary>
    private void LoadOrderNotes() {
      OrderNoteCollection orderNoteCollection = new OrderNoteController().FetchByOrderId(orderId);
      rptrNotes.DataSource = orderNoteCollection;
      rptrNotes.DataBind();
    }
    
    #endregion
    
    #endregion
    
  }
}